﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using BLL;
using Models;

namespace _2301MVCDemo.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        [Authorize]//特性  需要授权才能进入该方法
        public ActionResult Index()
        {
            List<Customer> list = CustomerManager.CustomersList();
            ViewBag.ulist = list;
            return View();
        }

        public ActionResult News(string year, string month, string day)
        {
            ViewBag.yyyy = year;
            ViewBag.MM = month;
            ViewBag.dd = day;
            return View();
        }

        public ActionResult LogOut()
        {
            FormsAuthentication.SignOut();//清除授权
            return RedirectToAction("Index","Login");//跨越控制器跳转
        }

        public ActionResult UserList()
        {
            List<Customer> list = CustomerManager.CustomersList();
            return View(list);
        }

        public ActionResult Del(string cid)
        {
            if (!CustomerManager.DeleteCustomerByID(cid))
            {
                Response.Write("<script>alert('删除失败！');</script>");
            }
            List<Customer> list = CustomerManager.CustomersList();
            return View("UserList", list);
        }

        public ActionResult Add(Customer customer)
        {
            if (!CustomerManager.AddCustomer(customer))
            {
                Response.Write("<script>alert('添加失败！');</script>");
            }
            List<Customer> list = CustomerManager.CustomersList();
            return View("UserList", list);
        }

        public ActionResult Search(string name)
        {
            List<Customer> list = CustomerManager.SearchCustomersByName(name);
            return View("UserList", list);
        }

        public ActionResult Edit(string cid)
        {
            Customer c = CustomerManager.SearchCustomersByID(cid);
            return View(c);
        }

        public ActionResult Save(Customer c)
        {
            if (CustomerManager.UpdateCustomer(c))
            {
                List<Customer> list = CustomerManager.CustomersList();
                return View("UserList", list);
            }
            else
            {
                Response.Write("<script>alert('保存失败！');</script>");
                Customer u = CustomerManager.SearchCustomersByID(c.CustomerID);
                return View("Edit", u);
            }
        }

        public ActionResult SaveEdit(Customer c)
        {
            if (!CustomerManager.UpdateCustomer(c))
            {
                Response.Write("<script>alert('保存失败！');</script>");
            }
            List<Customer> list = CustomerManager.CustomersList();
            return View("UserList", list);
        }
    }
}